﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using MyToDo.Shared;
using Newtonsoft.Json;
using RestSharp;

namespace MyToDo.Service
{
    public class HttpRestClient
    {
        private readonly string apiUrl;

        protected readonly RestClient client;

        public HttpRestClient(string apiUrl)
        {
            this.apiUrl = apiUrl;
            client = new RestClient(apiUrl);
        }

        public async Task<ApiResponse> ExecuteAsync(BaseRequest baseRequest)
        {
            var url = apiUrl + baseRequest.Route;
            var request = new RestRequest(url, baseRequest.Method);
            //request.AddHeader("Accept", "application/json");
            //request.AddHeader("Content-Type", baseRequest.ContentType);
            //request.AddParameter(
            //    "param",
            //    JsonConvert.SerializeObject(baseRequest.Parameter),
            //    ParameterType.RequestBody
            //);
            //var response = await client.ExecuteAsync(request);
            RestResponse response;
            if (baseRequest.Method == RestSharp.Method.Get)
            {
                request.AddObject(baseRequest.Parameter);
                response = await client.GetAsync(request);
            }
            else if (baseRequest.Method == RestSharp.Method.Post)
            {
                request.AddJsonBody(baseRequest.Parameter);
                response = await client.PostAsync(request);
            }
            else
            {
                request.Method = baseRequest.Method;
                response = await client.ExecuteAsync(request);
            }
            return JsonConvert.DeserializeObject<ApiResponse>(response.Content);
        }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="baseRequest"></param>
        /// <returns></returns>
        public async Task<ApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            //TODO:代码要优化
            var url = apiUrl + baseRequest.Route;
            var request = new RestRequest(url);
            RestResponse response;
            if (baseRequest.Method == RestSharp.Method.Get)
            {
                if (baseRequest.Parameter != null)
                {
                    request.AddObject(baseRequest.Parameter);
                }
                response = await client.GetAsync(request);
            }
            else if (baseRequest.Method == RestSharp.Method.Post)
            {
                request.AddJsonBody(baseRequest.Parameter);
                response = await client.PostAsync(request);
            }
            else
            {
                request.Method = baseRequest.Method;
                response = await client.ExecuteAsync(request);
            }

            return JsonConvert.DeserializeObject<ApiResponse<T>>(response.Content);
        }
    }
}
